Skip to content

Conversation

@davidclement90
Copy link
Contributor

@davidclement90 davidclement90 commented Apr 26, 2017

This adds the possibility to use external mapping in order to use custom ES mapping.
If you set USE_EXTERNAL_MAPPINGS to true, JanusGraph will check if the mapping is good instead of push it.
Whitout this adds, even if I push a mapping JanusGraph will update it during register phase.
This feature will allows to manage mapping directly in Kibana and not with ES_CREATE_EXTRAS_NS properties or to set for example a property with are declare in JanusGraph as String to IP, or use copy-to ES feature.

Issue : #222
Signed-off-by: David Clement david.clement90@laposte.net

@janusgraph-bot janusgraph-bot added the cla: yes This PR is compliant with the CLA label Apr 26, 2017
@davidclement90 davidclement90 force-pushed the elasticsearch-useExternalMapping branch from dd4bf0c to a33d9df Compare May 10, 2017 12:03
Copy link

@amcp amcp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor comments

Settings.Builder settings = Settings.builder();
if(useExternalMappings){
throw new IllegalArgumentException("Could not create index: " + indexName);
}else{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces before and after else

if (!client.indexExists(indexName)) {

Settings.Builder settings = Settings.builder();
if(useExternalMappings){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces after if and before the open brace

(map==Mapping.PREFIX_TREE && AttributeUtil.isGeo(dataType)),
"Specified illegal mapping [%s] for data type [%s]",map,dataType);

if(useExternalMappings){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing

} catch (IOException e) {
throw new PermanentBackendException(e);
}
}else{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing

try {
//We check if the externalMapping have the property 'key'
Map mappings = client.getMapping(indexName, store);
if(!mappings.containsKey(key)){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing


import java.util.Map;

public class RestIndexMappings {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add classdoc

@davidclement90 davidclement90 force-pushed the elasticsearch-useExternalMapping branch from a33d9df to 9a05a35 Compare May 18, 2017 09:18
@amcp
Copy link

amcp commented May 28, 2017

@sjudeng I think I just unblocked this PR

@sjudeng
Copy link
Contributor

sjudeng commented May 28, 2017

@davidclement90 Please rebase on master now that your other PR has been merged (thanks @amcp) and then I can add a review as well.

@davidclement90 davidclement90 force-pushed the elasticsearch-useExternalMapping branch 2 times, most recently from bdb4079 to 8aed813 Compare May 29, 2017 10:43
@davidclement90
Copy link
Contributor Author

Thanks for the code style updates on #233.
I try to respect the code style manually, i do not find yet a good config for my code formatter.

Copy link
Contributor

@sjudeng sjudeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another great contribution! Some comments below. On the minor code style side can you double check your indentation is consistently four spaces everywhere?

private void checkForOrCreateIndex(Configuration config) throws IOException {
Preconditions.checkState(null != client);

//Create index if it does not already exist
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this comment (it's copied inside the below if-then).

Preconditions.checkState(null != client);

//Create index if it does not already exist
if (!client.indexExists(indexName)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra nesting level by combining if-thens.

if (!client.indexExists(indexName) && !useExternalMappings) {
    //Create index if it does not already exist
} else if (!client.indexExists(indexName)) {
    throw new IllegalArgumentException("Could not create index: " + indexName);
}

port = getInterface() == ElasticSearchSetup.REST_CLIENT ? 9200 : 9300;
httpClient = HttpClients.createDefault();
try {
host = new HttpHost(InetAddress.getByName("127.0.0.1"), 9200);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use four spaces for indentation

Configuration indexConfig = config.restrictTo(INDEX_NAME);
FileInputStream fis = null;
try {
//Test create index KO mapping is not push
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use four spaces for indentation

assertTrue(res.getStatusLine().getStatusCode() >= 200);
assertTrue(res.getStatusLine().getStatusCode() < 300);
assertFalse(EntityUtils.toString(res.getEntity()).contains("error"));
}finally{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} finally {


private void executeRequest(HttpRequestBase request) throws IOException {
CloseableHttpResponse res = null;
try{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try {

public static final ConfigOption<Boolean> USE_EXTERNAL_MAPPINGS =
new ConfigOption<Boolean>(ES_CREATE_NS, "use-external-mappings",
"When JanusGraph register an index, JanusGraph can push the mapping or use an external mapping. " +
"Enabling this option make JanusGraph use an external mapping.", ConfigOption.Type.MASKABLE, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For description how about just "Whether JanusGraph should make use of an external mapping when registering an index." Or if you prefer yours make a few corrections: s/register/registers/, s/, JanusGraph/ it/ and s/make/makes/

public Map getMapping(String indexName, String typeName) throws IOException{
Response response = performRequest("GET", "/" + indexName.toLowerCase() + "/_mapping/"+typeName, null);
try (final InputStream inputStream = response.getEntity().getContent()) {
Map<String,Map<String,Map<String,RestIndexMappings>>> settings = mapper.readValue(inputStream, new TypeReference<Map<String, Map<String,Map<String,RestIndexMappings>>>>() {});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation to four spaces. Also you can update RestIndexMappings as shown in separate comment below and then you can simplify this deserialization code as follows.

Map<String,RestIndexMappings> settings = mapper.readValue(inputStream, new TypeReference<Map<String,RestIndexMappings>>() {});
return settings.get(indexName).getMappings().get(typeName).getProperties();

public void setProperties(Map<String, Object> properties) {
this.properties = properties;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this (single-type) mapping object down to inner class and use outer object for the (multi-type) mappings object.

public class RestIndexMappings {

    private Map<String,RestIndexMapping> mappings;

    public Map<String, RestIndexMapping> getMappings() {
        return mappings;
    }

    public void setMappings(Map<String, RestIndexMapping> mappings) {
        this.mappings = mappings;
    }

    public static class RestIndexMapping {

        private Map<String,Object> properties;

        public Map<String, Object> getProperties() {
            return properties;
        }

        public void setProperties(Map<String, Object> properties) {
            this.properties = properties;
        }
    }

}

@davidclement90 davidclement90 force-pushed the elasticsearch-useExternalMapping branch 2 times, most recently from 732343c to 7833bb0 Compare May 29, 2017 16:47
public static final ConfigOption<Boolean> USE_EXTERNAL_MAPPINGS =
new ConfigOption<Boolean>(ES_CREATE_NS, "use-external-mappings",
"Whether JanusGraph should make use of an external mapping when registering an index." +
"Enabling this option make JanusGraph use an external mapping.", ConfigOption.Type.MASKABLE, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the second sentence ("Enabling this option...") or else add a space after the preceding period and change "make" to "makes".

@davidclement90 davidclement90 force-pushed the elasticsearch-useExternalMapping branch from 7833bb0 to 403bf01 Compare May 30, 2017 06:24
@sjudeng
Copy link
Contributor

sjudeng commented May 30, 2017

@amcp Are your requested changes in here? I'll hold off merging #289 until this is merged since I think it's close.

@sjudeng
Copy link
Contributor

sjudeng commented Jun 3, 2017

@amcp Since it looks like you're looking over PRs now (thanks!!) can you also approve and/or merge this one if your comments are addressed?


public static final ConfigNamespace ELASTICSEARCH_NS =
new ConfigNamespace(INDEX_NS, "elasticsearch", "Elasticsearch index configuration");
new ConfigNamespace(INDEX_NS, "elasticsearch", "Elasticsearch index configuration");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove rogue spaces at end of line

port = getInterface() == ElasticSearchSetup.REST_CLIENT ? 9200 : 9300;
httpClient = HttpClients.createDefault();
try {
host = new HttpHost(InetAddress.getByName("127.0.0.1"), 9200);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you get the port on line 88 but ignore the value and use 9200 across the board. is this ok?

Copy link
Contributor Author

@davidclement90 davidclement90 Jun 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it's ok. Even if JanusGraph talks to Elasticsearch with the tranport API over port 9300, the mapping or the template are pushed with the REST API over port 9200.

@amcp
Copy link

amcp commented Jun 6, 2017

@davidclement90 @sjudeng I caught some more things as I reviewed the latest update.

Signed-off-by: David Clement <david.clement90@laposte.net>
@davidclement90 davidclement90 force-pushed the elasticsearch-useExternalMapping branch from 403bf01 to 5ffcdb6 Compare June 6, 2017 18:42
@amcp amcp merged commit 7ff31ce into JanusGraph:master Jun 7, 2017
@davidclement90 davidclement90 deleted the elasticsearch-useExternalMapping branch June 7, 2017 11:50
bwatson-rti-org pushed a commit to bwatson-rti-org/janusgraph that referenced this pull request Mar 9, 2019
…seExternalMapping

Can use external ES mapping instead of push mapping
micpod pushed a commit to micpod/janusgraph that referenced this pull request Nov 5, 2019
…seExternalMapping

Can use external ES mapping instead of push mapping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This PR is compliant with the CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants